//
//  AppDelegate.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/8.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.makeKeyAndVisible()
        
        registerNote()
        
        window?.backgroundColor = UIColor.whiteColor()
        
        setTheme()
        
//        let vc = ComposeViewController()
//        let nav = UINavigationController(rootViewController: vc)
        
        window?.rootViewController =  defaultRootController()
        
        return true
    }
    
    //注册通知
     func registerNote(){
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVc:", name: "tiaozhuan", object: nil)
        
    }
    
   func switchRootVc(n:NSNotification){
        
        if n.object != nil {
            
            window?.rootViewController = WelcomeViewController()
            
            return
        }
    
        window?.rootViewController = MainViewController()
    }
    
    func defaultRootController () -> UIViewController{
        
        if UserAccountViewModel().userLogin{
            
            if isNewVersion(){
                
                return  NewFeatureViewController()
            }else{
                
                return WelcomeViewController()
            }
            
            
        }
        return MainViewController()
    }
    
    private func isNewVersion () -> Bool{
        
        //获取新版本
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let versionNum = Double(versionStr)
        
        //获取旧版本
        let key = "sandBox"
        let userdefault = NSUserDefaults.standardUserDefaults()
        let lastNum = userdefault.doubleForKey(key)
        
        //保存新版本
        userdefault.setDouble(versionNum!, forKey: key)
        
        userdefault.synchronize()
        
        return versionNum > lastNum
        
    }
    
    
    func setTheme(){
        
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
        
    }

}

